#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define filename "log.txt"

int main()
{
    //int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    int fd = open(filename, O_RDONLY);
    if (fd < 0)
        perror("open");
    else
    {
        //dup2(fd, 1);
        //close(fd);
        //printf("fd = %d\n", fd);

        //const char* str = "hello world\n";
        //for (int i = 0; i < 3; ++i)
        //    write(1, str, strlen(str));
        ////for (int i = 0; i < 3;  ++i)
        ////    write(fd, str, strlen(str));
        
        dup2(fd, 0);
        close(fd);

        char buf[1024];
        ssize_t s = read(0, buf, sizeof(buf) - 1);
        if (s > 0)
        {
            buf[s] = '\0';
            printf("%s\n", buf);
        }
    }
    return 0;
}
